package com.fare.taluo.application.impl.crawl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.BaseEntity;
import com.fare.taluo.application.crawl.TaskAdapterApplication;
import com.fare.taluo.core.domain.config.channel.SourceEntity;
import com.fare.taluo.core.domain.crawl.AdapterOperate;
import com.fare.taluo.core.domain.crawl.AdapterUploadedEntity;
import com.fare.taluo.core.domain.crawl.TaskAdapterEntity;
import com.fare.taluo.vo.adapter.AdapterVO;
import com.fare.taluo.vo.core.TaskAdapterEntityVO;

/**
 * TaskAdapterApplication 实现类
 * @author Administrator
 */

@Transactional
@Scope("singleton")
@Service("taskAdapterApplication")
public class TaskAdapterApplicationImpl implements TaskAdapterApplication {

	@Override
	public void deleteTaskAdapterEntity(long id) throws Exception {
		TaskAdapterEntity adapter=BaseEntity.get(TaskAdapterEntity.class, id);
		adapter.remove();
	}
	@Override
	public void deleteAdapterUploadedEntity(long adapterUploadId)throws Exception {
		AdapterUploadedEntity uploadE=BaseEntity.get(AdapterUploadedEntity.class, adapterUploadId);
		uploadE.removeS();
	}

	public void saveM(TaskAdapterEntityVO vo) throws Exception{
		TaskAdapterEntity fpe = new TaskAdapterEntity();
		System.out.println("vo:"+vo);
		if(vo !=null){
		 SourceEntity source=BaseEntity.get(SourceEntity.class, vo.getSourceID());
		
//		 System.out.println("sourceId+++"+vo.getSourceID());
		fpe.setId(vo.getId());
//		System.out.println("sourceId+2+"+source);
		fpe.setSourceId(source);
		 System.out.println("sourceId++3++"+vo.getSourceID());
//		 fpe.setAdapterUploadId(adapterUploadId);
//		 System.out.println("adapterUploadId++++++"+vo.getAdapterUploadId());
		fpe.setAdapterName(vo.getAdapterName());
		fpe.setAdapterClass(vo.getAdapterClass());
		fpe.setIsenable(vo.getIsenable());
		fpe.setPriority(vo.getPriority());
		 fpe.setUpdatetime(new Date());
		 fpe.setCreattime(vo.getCreattime());
		 fpe.setAdapterUploadId(vo.getAdapterUploadId());
		 fpe.setRemarks(vo.getRemarks());
		 fpe.setVersion(vo.getVersion());
		}
		fpe.saveMorU(fpe);
	}
	
	@Override
	public void saveOrUpdateTaskAdapterEntity(TaskAdapterEntityVO adapterVO)
			throws Exception {
		 TaskAdapterEntity adapter=new TaskAdapterEntity();
		 
		 if(adapterVO!=null){
			 SourceEntity source=BaseEntity.get(SourceEntity.class, adapterVO.getSourceID());
			// AdapterUploadedEntity adUploadEntity=new AdapterUploadedEntity();
			 
		//	 AdapterUploadedEntity adId=BaseEntity.load(AdapterUploadedEntity.class, adUploadEntity.getId());
			 
//			 AdapterUploadedEntity adUploadEntity=BaseEntity.get(AdapterUploadedEntity.class,adapterVO.getAdapterUploadId());
//			 System.out.println("++++++++"+adUploadEntity);
//			 adapter.setAdapterUploadId(adUploadEntity.getId());
			 
			 System.out.println("adapter_upload_id:"+adapterVO.getAdapterUploadId());
			 adapter.setAdapterUploadId(adapterVO.getAdapterUploadId());
			 
			 
			 adapter.setSourceId(source);
			 adapter.setAdapterName(adapterVO.getAdapterName());
			 adapter.setAdapterClass(adapterVO.getAdapterClass());
			 adapter.setIsenable(adapterVO.getIsenable());
			 adapter.setPriority(adapterVO.getPriority());
			
			 //System.out.println("applicationImpl:进入增加操作");
			 adapter.setUpdatetime(new Date());
			 adapter.setCreattime(new Date());
			 adapter.setRemarks(adapterVO.getRemarks());
			 adapter.setVersion(adapterVO.getVersion());
			 
		 }
		 adapter.saveOrUpdateTaskInfoEntity();
	}

	@Override
	public List<TaskAdapterEntityVO> findTaskAdapterByConsdions(
			String sourceId, String isEnable,String adapterName, String adapterClass) {
		//System.out.println("TaskAdapterApplicationImpl:findTaskAdapterByConditions");
		TaskAdapterEntity adapter=new TaskAdapterEntity();
		List<TaskAdapterEntity> taskList=adapter.findTaskAdapterByConsdions(sourceId,isEnable ,adapterName, adapterClass);
		
		List<TaskAdapterEntityVO> voList=new ArrayList<TaskAdapterEntityVO>();
		//System.out.println("TaskAdapterApplicationImpl:"+taskList.size());
		
		if(taskList != null){
			if(taskList.size() > 0){
				for (TaskAdapterEntity task : taskList) {
					TaskAdapterEntityVO vo=new TaskAdapterEntityVO();
					vo.setId(task.getId());
					vo.setSourceID(task.getSourceId().getId());
					vo.setSourceName(task.getSourceId().getName());
					vo.setAdapterName(task.getAdapterName());
					vo.setAdapterClass(task.getAdapterClass());
					vo.setIsenable(task.getIsenable());
					vo.setPriority(task.getPriority());
					vo.setAdapterUploadId(task.getAdapterUploadId());
					//System.out.println("TaskAdapterApplicationImpl创建时间："+task.getCreattime().toString());
					
					vo.setCreattime(task.getCreattime());
					if(task.getUpdatetime()!=null){
						vo.setUpdatetime(task.getUpdatetime());
						//System.out.println("TaskAdapterApplicationImpl修改时间："+task.getUpdatetime().toString());
					}
					vo.setRemarks(task.getRemarks());
					vo.setVersion(task.getVersion());
					voList.add(vo);
				}
			}
		}
		
		return voList;
	}

	
	@Override
	public TaskAdapterEntityVO findTaskAdapterEntityVO(long id)
			throws Exception {
		TaskAdapterEntity adapter=BaseEntity.get(TaskAdapterEntity.class, id);
		
		TaskAdapterEntityVO vo=new TaskAdapterEntityVO();
		if(adapter!=null){
			vo.setId(adapter.getId());
			//vo.setSourceName(adapter.getSourceName());
			vo.setAdapterName(adapter.getAdapterName());
			vo.setAdapterClass(adapter.getAdapterClass());
			vo.setIsenable(adapter.getIsenable());
			vo.setPriority(adapter.getPriority());
			vo.setCreattime(adapter.getCreattime());
			vo.setUpdatetime(adapter.getUpdatetime());
			vo.setRemarks(adapter.getRemarks());
			vo.setVersion(adapter.getVersion());
		}
		return vo;
	}
	@Override
	public void uploadAdapter(AdapterVO vo) throws Exception {
		AdapterOperate ao=new AdapterOperate(vo);
		ao.addAdapter();
	}
	@Override
	public List<AdapterVO> findAdapterByConsdions(Long sourceId,
			Integer isEnable, String adapterClass) throws Exception {
		AdapterVO vo=new AdapterVO();
		vo.setSourceID(sourceId);
		vo.setIsenable(isEnable);
		vo.setAdapterClassName(adapterClass);
		AdapterOperate ao=new AdapterOperate(vo);
		List<TaskAdapterEntity> es=ao.findAdapterByConsdions();
		List<AdapterVO> vos=new ArrayList<AdapterVO>();
		if(es!=null && !es.isEmpty()){
			for(TaskAdapterEntity e:es){
				AdapterVO v=new AdapterVO();
				v.setId(e.getId());
				v.setSourceID(e.getSourceId().getId());
				v.setAdapterUploadId(e.getAdapterUploadId());
				v.setCreattime(e.getCreattime());
				v.setUpDataTime(e.getUpdatetime());
				v.setIsenable(Integer.valueOf(e.getIsenable()));
				v.setRemarks(e.getRemarks());
				v.setVersion(e.getVersion());
				v.setSourceName(e.getSourceId().getName());
				v.setAdapterClassName(e.getAdapterClass());
				vos.add(v);
			}
			
		}
		return vos;
	}
	@Override
	public boolean closeOrOpenAdapter(Long id, Integer isEnable)
			throws Exception {
		AdapterVO vo=new AdapterVO();
		vo.setId(id);
		vo.setIsenable(isEnable);
		AdapterOperate ao=new AdapterOperate(vo);
		return ao.closeOrOpenAdapter();
	}
	@Override
	public boolean deleteAdapter(Long id) throws Exception {
		AdapterVO vo=new AdapterVO();
		vo.setId(id);
		AdapterOperate ao=new AdapterOperate(vo);
		return ao.deleteAdapter();
	}
	@Override
	public AdapterVO downloadAdapterFileById(Long id) throws Exception {
		AdapterVO vo=new AdapterVO();
		vo.setAdapterUploadId(id);
		AdapterOperate ao=new AdapterOperate(vo);
		AdapterUploadedEntity adapter=ao.downloadAdapterFileDataById();
		if(adapter!=null){
			vo.setBlob(adapter.getAdapterData());
			vo.setAdapterClassName(adapter.getClassName());
			return vo;
		}else{
			throw new Exception("File isNot Exist!");
		}
		
	}
	
	
}
